package cn.mumzone.basic.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author Chaney
 * Date   2017/3/14 下午6:02.
 * EMail  qiuzhenhuan.hi@gmail.com
 */

public abstract class SimpleFragment extends Fragment {

    protected Activity mActivity;
    protected Context mContext;
    protected View mView;

    private boolean isFirstResume = true;

    private Unbinder mUnbinder;

    @Override
    public void onAttach(Context context) {
        this.mActivity = (Activity) context;
        this.mContext = context;
        super.onAttach(context);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstResume) {
            isFirstResume = false;
            return;
        }
        if (getUserVisibleHint() && !isHidden()) {
            onUserVisible();
        }
    }

    protected void onUserVisible(){};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayout(), null);
        mUnbinder = ButterKnife.bind(this, mView);
        initVariable();
        return mView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initEventAndData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }


    protected abstract void initVariable();

    protected abstract void initEventAndData();

    protected abstract int getLayout();
}
